<?php

namespace app\common\model;

use think\Model;


class UserVip extends Model
{


    // 表名
    protected $name = 'user_vip';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
//        'status_text',
//        'pay_time_text',
//        'pay_type_text'
    ];


    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }

    public function getPayTypeList()
    {
        return ['wechat' => __('Pay_type wechat'), 'alipay' => __('Pay_type alipay')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    /**
     * Account:sheng
     * User: Ixiangang
     * Date: 2025/2/12
     * Time: 10:06
     */
    public function notify($order_sn, $pay_price, $pay_sn = '', $paytype = '')
    {
        $uservip = self::get(['order_sn' => $order_sn]);
        if (!$uservip) return false;
        $uservip->status = '2';
        $uservip->pay_price = $pay_price;
        $uservip->pay_time = time();
        if ($pay_sn) {
            $uservip->pay_sn = $pay_sn;
        }
        if ($paytype) {
            $uservip->pay_type = $paytype;
        }
        $uservip->save();
        User::where('id', $uservip->user_id)->update(['vip' => '1']);
        return true;
    }

}
